int findUnit1(int unit_type = -1, int owner = cMyID, int index = -1)
{
    static int query = -1;
    if (query == -1)
    {
        query = kbUnitQueryCreate("Find1");
        kbUnitQuerySetState(query, cUnitStateAlive);
        kbUnitQuerySetIgnoreKnockedOutUnits(query, true);
    }
    
    if (index <= 0)
    {
        kbUnitQueryResetResults(query);
        kbUnitQuerySetUnitType(query, unit_type);
        if (owner >= 1000)
        {
            kbUnitQuerySetPlayerID(query, -1, false);
            kbUnitQuerySetPlayerRelation(query, owner);
        }
        else
        {
            kbUnitQuerySetPlayerRelation(query, -1);
            kbUnitQuerySetPlayerID(query, owner, false);
        }
        int number = kbUnitQueryExecute(query);
        if (index <= -1)
            return(kbUnitQueryGetResult(query, aiRandInt(number)));
    }
    
    return(kbUnitQueryGetResult(query, index));
}


int findUnit2(int unit_type = -1, int owner = cMyID, int index = -1)
{
    static int query = -1;
    if (query == -1)
    {
        query = kbUnitQueryCreate("Find2");
        kbUnitQuerySetState(query, cUnitStateAlive);
        kbUnitQuerySetIgnoreKnockedOutUnits(query, true);
    }
    
    if (index <= 0)
    {
        kbUnitQueryResetResults(query);
        kbUnitQuerySetUnitType(query, unit_type);
        if (owner >= 1000)
        {
            kbUnitQuerySetPlayerID(query, -1, false);
            kbUnitQuerySetPlayerRelation(query, owner);
        }
        else
        {
            kbUnitQuerySetPlayerRelation(query, -1);
            kbUnitQuerySetPlayerID(query, owner, false);
        }
        int number = kbUnitQueryExecute(query);
        if (index <= -1)
            return(kbUnitQueryGetResult(query, aiRandInt(number)));
    }
    
    return(kbUnitQueryGetResult(query, index));
}


int findUnit3(int unit_type = -1, int owner = cMyID, int index = -1)
{
    static int query = -1;
    if (query == -1)
    {
        query = kbUnitQueryCreate("Find3");
        kbUnitQuerySetState(query, cUnitStateAlive);
        kbUnitQuerySetIgnoreKnockedOutUnits(query, true);
    }
    
    if (index <= 0)
    {
        kbUnitQueryResetResults(query);
        kbUnitQuerySetUnitType(query, unit_type);
        if (owner >= 1000)
        {
            kbUnitQuerySetPlayerID(query, -1, false);
            kbUnitQuerySetPlayerRelation(query, owner);
        }
        else
        {
            kbUnitQuerySetPlayerRelation(query, -1);
            kbUnitQuerySetPlayerID(query, owner, false);
        }
        int number = kbUnitQueryExecute(query);
        if (index <= -1)
            return(kbUnitQueryGetResult(query, aiRandInt(number)));
    }
    
    return(kbUnitQueryGetResult(query, index));
}


int findUnitByLocation1(int unit_type = -1, int owner = cMyID, vector location = cInvalidVector, float radius = 0.0, int index = -1)
{
    static int query = -1;
    if (query == -1)
    {
        query = kbUnitQueryCreate("FindByLoc1");
        kbUnitQuerySetState(query, cUnitStateAlive);
        kbUnitQuerySetIgnoreKnockedOutUnits(query, true);
        kbUnitQuerySetAscendingSort(query, true);
    }
    
    if (index <= 0)
    {
        kbUnitQueryResetResults(query);
        kbUnitQuerySetUnitType(query, unit_type);
        kbUnitQuerySetPosition(query, location);
        kbUnitQuerySetMaximumDistance(query, radius);
        if (owner >= 1000)
        {
            kbUnitQuerySetPlayerID(query, -1, false);
            kbUnitQuerySetPlayerRelation(query, owner);
        }
        else
        {
            kbUnitQuerySetPlayerRelation(query, -1);
            kbUnitQuerySetPlayerID(query, owner, false);
        }
        int number = kbUnitQueryExecute(query);
        if (index <= -1)
            return(kbUnitQueryGetResult(query, aiRandInt(number)));
    }
    
    return(kbUnitQueryGetResult(query, index));
}


int findUnitByLocation2(int unit_type = -1, int owner = cMyID, vector location = cInvalidVector, float radius = 0.0, int index = -1)
{
    static int query = -1;
    if (query == -1)
    {
        query = kbUnitQueryCreate("FindByLoc2");
        kbUnitQuerySetState(query, cUnitStateAlive);
        kbUnitQuerySetIgnoreKnockedOutUnits(query, true);
        kbUnitQuerySetAscendingSort(query, true);
    }
    
    if (index <= 0)
    {
        kbUnitQueryResetResults(query);
        kbUnitQuerySetUnitType(query, unit_type);
        kbUnitQuerySetPosition(query, location);
        kbUnitQuerySetMaximumDistance(query, radius);
        if (owner >= 1000)
        {
            kbUnitQuerySetPlayerID(query, -1, false);
            kbUnitQuerySetPlayerRelation(query, owner);
        }
        else
        {
            kbUnitQuerySetPlayerRelation(query, -1);
            kbUnitQuerySetPlayerID(query, owner, false);
        }
        int number = kbUnitQueryExecute(query);
        if (index <= -1)
            return(kbUnitQueryGetResult(query, aiRandInt(number)));
    }
    
    return(kbUnitQueryGetResult(query, index));
}


int findUnitByLocation3(int unit_type = -1, int owner = cMyID, vector location = cInvalidVector, float radius = 0.0, int index = -1)
{
    static int query = -1;
    if (query == -1)
    {
        query = kbUnitQueryCreate("FindByLoc3");
        kbUnitQuerySetState(query, cUnitStateAlive);
        kbUnitQuerySetIgnoreKnockedOutUnits(query, true);
        kbUnitQuerySetAscendingSort(query, true);
    }
    
    if (index <= 0)
    {
        kbUnitQueryResetResults(query);
        kbUnitQuerySetUnitType(query, unit_type);
        kbUnitQuerySetPosition(query, location);
        kbUnitQuerySetMaximumDistance(query, radius);
        if (owner >= 1000)
        {
            kbUnitQuerySetPlayerID(query, -1, false);
            kbUnitQuerySetPlayerRelation(query, owner);
        }
        else
        {
            kbUnitQuerySetPlayerRelation(query, -1);
            kbUnitQuerySetPlayerID(query, owner, false);
        }
        int number = kbUnitQueryExecute(query);
        if (index <= -1)
            return(kbUnitQueryGetResult(query, aiRandInt(number)));
    }
    
    return(kbUnitQueryGetResult(query, index));
}


int countUnitsByLocation(int unit_type = -1, int owner = cMyID, vector location = cInvalidVector, float radius = 0.0)
{
    static int query = -1;
    if (query == -1)
    {
        query = kbUnitQueryCreate("CountByLoc");
        kbUnitQuerySetState(query, cUnitStateAlive);
        kbUnitQuerySetIgnoreKnockedOutUnits(query, true);
    }
    
    kbUnitQueryResetResults(query);
    kbUnitQuerySetUnitType(query, unit_type);
    kbUnitQuerySetPosition(query, location);
    kbUnitQuerySetMaximumDistance(query, radius);
    if (owner >= 1000)
    {
        kbUnitQuerySetPlayerID(query, -1, false);
        kbUnitQuerySetPlayerRelation(query, owner);
    }
    else
    {
        kbUnitQuerySetPlayerRelation(query, -1);
        kbUnitQuerySetPlayerID(query, owner, false);
    }
    
    return(kbUnitQueryExecute(query));
}


extern int gDoubleQuery = -1;
int doubleQuery(int query = -1, int unit_type = -1, vector location = cInvalidVector, float radius = 200.0, int index = 0)
{
    
    if (gDoubleQuery == -1)
        gDoubleQuery = kbUnitQueryCreate("FindInQuery");
    
    if (index == 0)
    {
        kbUnitQueryResetResults(gDoubleQuery);
        kbUnitQuerySetUnitType(gDoubleQuery, unit_type);
        kbUnitQuerySetPosition(gDoubleQuery, location);
        kbUnitQuerySetMaximumDistance(gDoubleQuery, radius);
        kbUnitQuerySetAscendingSort(gDoubleQuery, true);
        kbUnitQueryExecuteOnQuery(gDoubleQuery, query);
    }
    
    return(kbUnitQueryGetResult(gDoubleQuery, index));
}